package com.per.note.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.per.note.R;
import com.per.note.db.SqliteManage;
import com.per.note.db.UserDao;
import com.per.note.ui.load.MyApplication;
import com.per.note.ui.main.MainActivity;

public class LoginActivity extends AppCompatActivity {

    SqliteManage sqliteManage = SqliteManage.getInstance(this);
    UserDao userDao = new UserDao(this);
    private EditText mEditTextName;// 账号
    private EditText mEditTextPwd;// 密码
    private Button mButtonOK;
    private TextView mTextViewRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("-----","onCreate 创建");
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题
        setContentView(R.layout.activity_login);
        Log.i("-----","setContentView 创建");
//        LoginOk();

        mEditTextName = (EditText) this.findViewById(R.id.et_login_name);
        mEditTextPwd = (EditText) this.findViewById(R.id.et_login_password);
        mButtonOK = (Button) this.findViewById(R.id.bt_login_ok);
        mTextViewRegister = (TextView) this.findViewById(R.id.tv_login_register_link);
    }


    // 按钮对应的点击事件
    // 参数 v 代表的就是当前被点击的条目对应的view对象
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login_ok:// 登录按钮
                // 相应按钮的点击事件)
                if (mEditTextName.getText().toString().trim().equals("")) 	{
                    Toast.makeText(getApplicationContext(), "账户名不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                if (mEditTextPwd.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(), "密码不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                //判断用户是否存在
                boolean result = userDao.find(mEditTextName.getText().toString());

                if (result) {
                    //判断登录结果
                    result = userDao.findLogin(mEditTextName.getText().toString(),mEditTextPwd.getText().toString());
                    if (result) {
                        //修改登录状态为已登录，修改全局变量当前登录用户id
                        userDao.updateLoginOK(MyApplication.userid);
                        //登录成功，跳转页面 MainActiity.class
                        Toast.makeText(this,"登录成功", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        this.startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(), "密码有误", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "不存在该账号", Toast.LENGTH_LONG).show();
                }
                break;

            case R.id.tv_login_register_link:
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_login_clear:
                mEditTextName.setText("");
                mEditTextPwd.setText("");
                break;
        }

    }
}
